package com.zhiyun.yycall.ui;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.gzit.utils.AndroidUtils;
import com.gzit.utils.StringUtils;
import com.zhiyun.common.YYCallUtils;
import com.zhiyun.message.LoginRequest;
import com.zhiyun.message.LoginResponse;
import com.zhiyun.net.AsynCallback;
import com.zhiyun.net.Net;
import com.zhiyun.net.Response;
import com.zhiyun.trace.TraceActivity;
import com.zhiyun.yycall.Config;
import com.zhiyun.yycall.R;
import com.zhiyun.yycall.YYCallController;
import com.zhiyun.yycall.version.VersionManager;

public class SplashActivity extends TraceActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.splash_activity);

		if (!callFromOtherApp()) {
			autoLogin();
		}

		VersionManager.getInstance().startTask();

	}

	private boolean callFromOtherApp() {

		// 从其它通讯录应用唤醒YYCall
		Intent data = this.getIntent();
		if (data != null) {
			Uri uri = data.getData();
			if (uri != null) {
				final String targetNo = uri.getSchemeSpecificPart();
				if (!StringUtils.isBlank(targetNo)) {

					// 网络不可用，提示用户是否切换到正常拨号形式
					if (!AndroidUtils.System.isNetworkAvailable()) {
						AndroidUtils.Dialog.confirm(this, "云之呼温馨提示",
								"当前网络不可用，是否切换回【系统拨号】模式？",
								new OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {

										YYCallController.startNormalCall(
												SplashActivity.this, targetNo);
										trace("call_from_outside_app", "normal");
										finish();

									}
								}, new OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										trace("call_from_outside_app", "fail");
										finish();

									}
								});

						return true;

					}

					// 满足YYCall拨号条件
					String phoneNo = Config.getPhoneNo();
					String password = Config.getPassword();

					// String phoneNo = "13590115926";
					// String password = "zs";

					if (StringUtils.isAllNotBlank(phoneNo, password)) {
						YYCallController.createController(this).startCallback(
								targetNo);
						trace("call_from_outside_app", "callback");
						this.finish();
						return true;
					}

				}
			}

		}

		trace("call_from_outside_app", "fail");

		return false;

	}

	private void autoLogin() {
		String phoneNo = Config.getPhoneNo();
		String password = Config.getPassword();
		if (StringUtils.isAllNotBlank(phoneNo, password)) {
			remoteLogin(phoneNo, password);
			return;
		}

		this.gotoLoginActivity();

	}

	private void remoteLogin(final String phoneNo, final String password) {

		LoginRequest req = new LoginRequest();
		req.setPhoneNo(phoneNo);
		req.setPassword(password);

		Net.doSimpleHttpRemoter(req, new LoginResponse(),
				new AsynCallback<Response>() {

					@Override
					public boolean callback(Response r) {
						if (r.isFail()) {
							switch (r.getErrorCode()) {
							case Response.WAITTING_ACTIVE:// 用户处于待激活状态，直接进入激活界面
								gotoActiveActivity();
								break;
							default:
								gotoLoginActivity();

							}

							return true;
						}
						gotoMainActivity();
						return true;

					}
				});

	}

	private final static int ACTIVATE_CODE = 2;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case ACTIVATE_CODE:
				this.gotoMainActivity();
				break;
			}

		} else {
			switch (requestCode) {
			case ACTIVATE_CODE:
				this.finish();
				break;
			}

		}

	}

	private void gotoLoginActivity() {
		Intent intent = new Intent();
		intent.setClass(this, LoginActivity.class);
		this.startActivity(intent);
		this.finish();
	}

	private void gotoMainActivity() {
		Intent intent = new Intent();
		intent.setClass(this, MainActivity.class);
		this.startActivity(intent);
		this.finish();

	}

	private void gotoActiveActivity() {
		Intent intent = new Intent();
		intent.setClass(this, ActivateActivity.class);
		intent.putExtra("phoneNo", Config.getPhoneNo());
		this.startActivityForResult(intent, ACTIVATE_CODE);
	}

}
